<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 小虎哥 <1105415366@qq.com>
 * Date: 2018-06-28
 */

namespace weapp\MonkWeixin\controller;

use think\Db;
use think\Cache;
use app\common\controller\Weapp;
use weapp\MonkWeixin\model\MonkWeixinModel;
use weapp\MonkWeixin\logic\MonkWeixinLogic;

/**
 * 插件的控制器
 */
class MonkWeixin extends Weapp
{
    /**
     * 实例化模型
     */
    private $model;
    private $logic;
    private $weappData;
    private $weappInfo;
    public function __construct()
    {
        parent::__construct();
        /*插件基本信息*/
        $this->weappInfo = $this->getWeappInfo();
        $this->assign('weappInfo', $this->weappInfo);
        /*--end*/
        $this->model = new \weapp\MonkWeixin\model\MonkWeixinModel();
        $this->weappData = $this->model->weappData();
        $this->logic = new \weapp\MonkWeixin\logic\MonkWeixinLogic();
    }
    /**
     * 插件使用指南
     */
    public function doc(){
        return $this->fetch('doc');
    }

    /**
     * 系统内置钩子show方法（没用到这个方法，建议删掉）
     * 用于在前台模板显示片段的html代码，比如：QQ客服、对联广告等
     *
     * @param  mixed  $params 传入的参数
     */
    public function show($params = null){
        $row = isset($this->weappData['data']) ? $this->weappData['data'] : [];
        $this->assign('row', $row);
        echo $this->fetch('show');
    }

    /**
     * 插件后台管理 - 列表
     */
    public function index()
    {
        if (IS_POST) {
            $post = input('post.');                 
            if (empty(array_filter($post['proimg']))) {
                $this->error("请填写微信号/二维码",'','',2);
            }        
            foreach ($post['proimg'] as $key => $value) {        
                if($value[1]&&$value[0]){
                    $datas[]=['wechat_name'=>$value[1],'code_img'=>$value[0]];
                }                                
            }      
            unset($post['proimg']);                     
            $post['proimg'] =$datas;    
            if($post['ids']){
              $post['ids']=str_replace('，', ',', $post['ids']);
            }
            $tag_weapp = $post['tag_weapp'];   
            $data = array(
                'data'        => json_encode($post),
                'update_time' => getTime(),
                'tag_weapp'  => $tag_weapp,
            );                        
            $r = M('weapp')->where('code','eq','MonkWeixin')->update($data);
            if ($r) {
                \think\Cache::clear('hooks');
                Cache::rm('MonkWeixin');
                cookie('no_popup', null);
                adminLog('编辑'.$this->weappInfo['name'].'：插件配置'); // 写入操作日志
                $this->success("操作成功!", weapp_url('MonkWeixin/MonkWeixin/index'));
            }            
            $this->error("操作失败!");
        }
        $row = $this->weappData['data'];        
        try {
            $this->logic->insertAction('function');                  
            $this->logic->insertAction('view');                  
        } catch (\Exception $e){
        }           
        if(isset($row['is_typeids'])){
            $selectdata = $row['is_typeids'];
        }else{
            $selectdata = 0;
        }
        /*允许编辑的栏目*/
        $allow_release_channel = Db::name('channeltype')->where(['id'=>['not in',[6,8,51]]])->column('id');
        $select_html = allow_release_arctype($selectdata, $allow_release_channel);
        $this->assign('select_html', $select_html);
        $this->assign('row', $row);
        $this->assign('info',  $this->weappData);
        return $this->fetch('index');
    }    
}